/*
 * VHClubMatchesTab.java
 * Created on 23 sept. 2009
 *
 * VirtualHockey Internet Platform
 * http://code.google.com/p/virtualhockey/
 *
 * Copyright (c) 2005-2009, Jens and Jan Janke (VirtualHockey Project)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
 */

package com.hockeo.vhweb.client.ui.tabs;

import java.util.List;

import com.extjs.gxt.ui.client.Style;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.hockeo.vhweb.client.VHMain;
import com.hockeo.vhweb.client.mvc.VHController;
import com.hockeo.vhweb.client.rpc.VHRpcCallback;
import com.hockeo.vhweb.client.ui.base.VHTabItem;
import com.hockeo.vhweb.client.ui.widget.VHClubMatchesGrid;
import com.hockeo.vhweb.client.ui.widget.VHMatchTypeSelector;
import com.hockeo.vhweb.client.ui.widget.VHMatchlistGroupingSelector;
import com.hockeo.vhweb.client.utils.VHJsData;
import com.hockeo.vhweb.client.utils.enums.VHJsMatchType;
import com.hockeo.vhweb.client.utils.enums.VHJsMatchlistGroupingType;

/**
 * Displays all matches of the season of a single selected group. The match list can be
 * grouped either by moveday or by contest.
 *
 * @version $Id$
 * @author jjanke
 */
public class VHClubMatchesTab extends VHTabItem
{
  private VHJsData                    d_club;
  private VHJsMatchlistGroupingType   d_groupingLast;
  private VHJsMatchType               d_matchTypeLast;
  private VHMatchTypeSelector         d_matchTypeSelector;
  private VHMatchlistGroupingSelector d_matchlistGroupingSelector;
  private VHClubMatchesGrid           d_matchGrid;
  private boolean                     d_fClubChanged;

  public VHClubMatchesTab( VHController controller, VHJsData club )
  {
    super( controller, VHMain.getConstants().clubSeasonMatches() );

    d_club = club;

    setIcon( VHMain.ICON_MATCH_LIST );
  }

  public void setClub( VHJsData club )
  {
    if ( d_club != null && d_club.getId().equals( club.getId() ) )
      return;

    d_club = club;

    // Ask to refresh the displayed match list
    //
    d_groupingLast = null;
    d_matchTypeLast = null;
    d_fClubChanged = true;
  }

  @Override
  public void buildContent()
  {
    BorderLayoutData data;

    if ( isContentReady() )
    {
      // If a refresh has been requested, initiate it by re-selecting the default grouping
      //
      if ( d_groupingLast == null )
      {
        // Select default match type while event processing is disabled. This is to avoid
        // an immediate data load which will be triggered by resetting the grouping later.
        //
        d_matchTypeSelector.getCombobox().disableEvents( true );
        d_matchTypeSelector.selectDefault();
        d_matchTypeSelector.getCombobox().disableEvents( false );

        d_matchlistGroupingSelector.selectDefault();
      }

      return;
    }

    setLayoutOnChange( true );

    // Northern settings panel (contest type selector)
    //
    ContentPanel panelNorth = new ContentPanel( new RowLayout( Style.Orientation.VERTICAL ) );
    panelNorth.setHeaderVisible( false );

    d_matchTypeSelector = new VHMatchTypeSelector();
    panelNorth.add( d_matchTypeSelector, new RowData( Style.DEFAULT, Style.DEFAULT, new Margins( 4, 4, 4, 4 ) ) );

    d_matchlistGroupingSelector = new VHMatchlistGroupingSelector();
    panelNorth.add( d_matchlistGroupingSelector, new RowData( Style.DEFAULT, Style.DEFAULT, new Margins( 0, 4, 4, 4 ) ) );

    data = new BorderLayoutData( LayoutRegion.NORTH );
    data.setMargins( new Margins( 4, 4, 4, 4 ) );
    data.setSize( 58f );
    add( panelNorth, data );

    // event listeners
    //
    d_matchTypeSelector.getCombobox().addSelectionChangedListener( new SelectionChangedListener<VHJsData>() {
      @Override
      public void selectionChanged( SelectionChangedEvent<VHJsData> se )
      {
        VHJsData matchType = se.getSelectedItem();
        onMatchTypeSelected( matchType );
      }
    } );

    d_matchlistGroupingSelector.getCombobox().addSelectionChangedListener( new SelectionChangedListener<VHJsData>() {
      @Override
      public void selectionChanged( SelectionChangedEvent<VHJsData> se )
      {
        VHJsData grouping = se.getSelectedItem();
        onGroupingSelected( grouping );
      }
    } );

    // Reselect the default grouping value so that initial data loading is triggered
    //
    d_matchlistGroupingSelector.selectDefault();

    super.buildContent();
  }

  /**
   * Sets the main panel area based on the selected grouping.
   *
   * This method should be called when the grouping selector has received its data and
   * then every time the user chooses a different grouping.
   */
  public void onGroupingSelected( VHJsData groupingSelected )
  {
    VHJsMatchlistGroupingType grouping = VHJsMatchlistGroupingType.valueOf( groupingSelected.getId() );
    VHJsMatchType matchType = VHJsMatchType.valueOf( d_matchTypeSelector.getCombobox().getValue().getId() );

    // don't do anything if the grouping did not change
    //
    if ( !d_fClubChanged && d_groupingLast != null && d_groupingLast == grouping )
      return;

    d_groupingLast = grouping;

    // create match grid if necessary
    //
    if ( d_matchGrid == null )
    {
      d_matchGrid = new VHClubMatchesGrid( getController(), d_club, grouping, matchType );

      BorderLayoutData data = new BorderLayoutData( LayoutRegion.CENTER );
      data.setMargins( new Margins( 0, 4, 4, 4 ) );

      add( d_matchGrid, data );

      VHController.getBaseDataService().getMatchesForClub( VHController.getLocale(), d_club.getId(),
          new VHRpcCallback<List<VHJsData>>( d_matchGrid, null ) );
    }
    else if ( d_fClubChanged )
    {
      d_fClubChanged = false;
      d_matchGrid.setClub( d_club );
      d_matchGrid.setGrouping( grouping );
      d_matchGrid.setMatchType( matchType );

      // reload matches
      //
      VHController.getBaseDataService().getMatchesForClub( VHController.getLocale(), d_club.getId(),
          new VHRpcCallback<List<VHJsData>>( d_matchGrid, null ) );
    }
    else
    {
      // it must be a grouping change only
      //
      d_matchGrid.setGrouping( grouping );
    }
  }

  /**
   * Sets the main panel area based on the selected grouping.
   *
   * This method should be called when the grouping selector has received its data and
   * then every time the user chooses a different grouping.
   */
  public void onMatchTypeSelected( VHJsData matchTypeSelected )
  {
    VHJsMatchlistGroupingType grouping = VHJsMatchlistGroupingType.valueOf( d_matchlistGroupingSelector.getCombobox().getValue().getId() );
    VHJsMatchType matchType = VHJsMatchType.valueOf( matchTypeSelected.getId() );

    // don't do anything if the match type did not change
    //
    if ( !d_fClubChanged && d_matchTypeLast != null && d_matchTypeLast == matchType )
      return;

    d_matchTypeLast = matchType;

    // create match grid if necessary
    //
    if ( d_matchGrid == null )
    {
      d_matchGrid = new VHClubMatchesGrid( getController(), d_club, grouping, matchType );

      BorderLayoutData data = new BorderLayoutData( LayoutRegion.CENTER );
      data.setMargins( new Margins( 0, 4, 4, 4 ) );

      add( d_matchGrid, data );

      VHController.getBaseDataService().getMatchesForClub( VHController.getLocale(), d_club.getId(),
          new VHRpcCallback<List<VHJsData>>( d_matchGrid, null ) );
    }
    else if ( d_fClubChanged )
    {
      d_fClubChanged = false;
      d_matchGrid.setClub( d_club );
      d_matchGrid.setGrouping( grouping );
      d_matchGrid.setMatchType( matchType );

      // reload matches
      //
      VHController.getBaseDataService().getMatchesForClub( VHController.getLocale(), d_club.getId(),
          new VHRpcCallback<List<VHJsData>>( d_matchGrid, null ) );
    }
    else
    {
      // it must be a match type change only
      //
      d_matchGrid.setMatchType( matchType );
    }
  }
}
